package com.yl.springbootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yangl
 */
@Configuration
public class TtlQueueConfig {
    /**
     * 普通交换机名称
     */

    public static final String X_EXCHANGE = "X";
    /**
     * 死信交换机名称
     */
    public static final String Y_DEAD_EXCHANGE = "Y";
    /**
     * 普通队列的名称
     */
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";
    public static final String QUEUE_C = "QC";
    /**
     * 死信队列的名称
     */
    public static final String DEAD_LETTER_QUEUE = "QD";

    /**
     * 声明普通交换机X
     * @return
     */
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    /**
     * 声明死信交换机Y
     * @return
     */
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_EXCHANGE);
    }

    /**
     * 声明队列A
     * @return
     */
    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> map = new HashMap<>(3);
        //设置死信交换机
        map.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        //设置死信routingKey
        map.put("x-dead-letter-routing-key", "YD");
        //设置延迟时间
        map.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(map).build();
    }

    /**
     * 声明队列B
     * @return
     */
    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> map = new HashMap<>(3);
        //设置死信交换机
        map.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        //设置死信routingKey
        map.put("x-dead-letter-routing-key", "YD");
        //设置延迟时间
        map.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(map).build();
    }


    /**
     * 声明死信队列
     * @return
     */
    @Bean("queueD")
    public Queue queueD(){
        return new Queue(DEAD_LETTER_QUEUE);
    }

    /**
     * 队列A和普通交换机X绑定
     * @param queueA
     * @param xExchange
     * @return
     */
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA, @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    /**
     * 队列B和普通交换机X绑定
     * @param queueB
     * @param xExchange
     * @return
     */
    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB, @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    /**
     * 队列A和死信队列Y绑定
     * @param queueA
     * @param yExchange
     * @return
     */
    @Bean
    public Binding queueABindingY(@Qualifier("queueB") Queue queueA, @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueA).to(yExchange).with("YD");
    }

    /**
     * 队列B和死信队列Y绑定
     * @param queueB
     * @param yExchange
     * @return
     */
    @Bean
    public Binding queueBBindingY(@Qualifier("queueB") Queue queueB, @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueB).to(yExchange).with("YD");
    }


    /**
     * 队列C和死信队列Y绑定
     * @param queueC
     * @param yExchange
     * @return
     */
    @Bean
    public Binding queueCBindingY(@Qualifier("queueC") Queue queueC, @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueC).to(yExchange).with("XC");
    }

    @Bean
    public Binding deadLetterBindingQAD(@Qualifier("queueD") Queue queueD, @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }
}
